package mssql

import (
	"fmt"
	"gorm.io/driver/sqlserver"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"server/global"
)

func InitDB() (*gorm.DB, error) {

	var gormConfig gorm.Config
	if global.GL_CONFIG.Sql.LogMode { //是否打印SQl语句
		gormConfig.Logger = logger.Default.LogMode(logger.Info)
	}
	gormConfig.DisableForeignKeyConstraintWhenMigrating = true

	// 配置dsn
	dsn := fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s&encrypt=disable",
		global.GL_CONFIG.Sql.User, global.GL_CONFIG.Sql.Pass, global.GL_CONFIG.Sql.Host, global.GL_CONFIG.Sql.Port, global.GL_CONFIG.Sql.DataBase)
	db, err := gorm.Open(sqlserver.New(sqlserver.Config{
		DSN: dsn, // DSN data source name

	}), &gormConfig)

	if err != nil {
		panic("数据库链接错误")
		return db, err
	}
	return db, nil
}
